/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-10 12:14:27
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-11 16:14:37
 * @FilePath: /yt-devops/lib/pages/mine/system_set/system_set_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// ignore_for_file: unused_local_variable, prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/mine/system_set/system_set_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';

class SystemSetPage extends GetView<SystemSetController> {
  SystemSetPage({Key? key}) : super(key: key);
  @override
  final controller = Get.put(SystemSetController());
  final state = Get.find<SystemSetController>().state;

  Widget _generateItem(context, list, index) {
    return PermissionWidget(
        permissionStringList: [
          index == 0
              ? PermissionConstant.mineSystemSettingMultilingual
              : PermissionConstant.mineSystemSettingAccount
        ],
        child: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () => controller.onClick(index),
          child: Container(
            // color: Colour.white,
            clipBehavior: Clip.hardEdge,
            margin: const EdgeInsets.only(left: 16, right: 16),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
            ),
            child: ListTile(
              dense: true,
              minLeadingWidth: 0,
              title: Text(
                list[index],
                style: TextStyle(color: Colour.mainTextColor, fontSize: 16.sp),
              ),
              trailing: index == 0
                  ? Container(
                      width: 150.w,
                      // color: Colors.red,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Text(
                            controller.languageString,
                            style: TextStyle(
                                color: Colour.lightTextColor, fontSize: 16.sp),
                          ),
                          Image.asset(ImageAssets.rightArrow)
                        ],
                      ),
                    )
                  : Image.asset(ImageAssets.rightArrow),
            ),
          ),
        ));
  }

  Widget _generateLogout(context, list, index) {
    return Container(
      clipBehavior: Clip.hardEdge,
      margin: const EdgeInsets.only(left: 16, right: 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
      ),
      child: ListTile(
        dense: true,
        minLeadingWidth: 0,
        title: Center(
          child: Text(
            list[index],
            style: TextStyle(
              color: Colour.orangeTextColor,
              fontSize: 16.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        onTap: () async {
          controller.throttleFunc.throttle(() async {
            controller.logout();
          });
          await Future<void>.delayed(const Duration(seconds: 1));
        },
      ),
    );
  }

  Widget _generateList(List list) {
    return ListView.builder(
      padding: EdgeInsets.only(top: 10),
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        if (index == list.length - 1) {
          return Container(
            margin: EdgeInsets.symmetric(vertical: 10),
            child: _generateLogout(context, list, index),
          );
        }

        return _generateItem(context, list, index);
      },
      itemCount: list.length,
    );
  }

  @override
  Widget build(BuildContext context) {
    List<String> list = [
      ResKey.multilingual.tr,
      ResKey.accountSecurity.tr,
      ResKey.logout.tr,
    ];
    if (Get.locale != null && Get.locale!.languageCode.toString() == 'en') {
      controller.languageString = 'English';
    } else {
      controller.languageString = '中文(简体)';
    }
    return GetBuilder<SystemSetController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Colour.backgroundColor,
          appBar: AppBarWidget.defaultAppBar(
            ResKey.systemSetting.tr,
          ),
          body: _generateList(list),
        );
      },
    );
  }
}
